#字符串重复统计
#字符表'abcdefghijklmnopqrstuvwxyz'
#随机挑选2个字母组成字符串，共挑选100个
#降序输出所有不同的字符串及重复的次数

import random
from string import ascii_lowercase
l = 100
chars = [''.join(random.choices(ascii_lowercase,k=2)) for i in range(l)]
print(chars)

# 1. 使用字典统计排序
from collections import defaultdict
d = defaultdict(lambda :0)
for i in chars:
    d[i] += 1
print(sorted(d.items(),reverse=True))

print('#' * 80)

<<<<<<< HEAD
#2. 使用列表统计排序,效率低
=======
# 2. 使用列表统计排序,效率低
>>>>>>> 4e899bc425b8147a889596c546faf900d4eeb29f
count_chars = []
state = [False] * l

for i in range(l):
    count = 1
    if state[i]: continue
    for j in range(i+1,l):
        if state[j]: continue
        if chars[i] == chars[j]:
            count += 1
            state[j] = True
    if count != 1:
        state[i] = True
        count_chars.append((chars[i],count))
    else:
        count_chars.append((chars[i], count))
#print(count_chars)
print(sorted(count_chars,reverse=True))


# 没有做到按照出现次数排序，可以改进一下

# 还是没有按次数排序
